home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 004 / gstobject / set / Example / c / main < prev   
Text File  |  1994-09-24  |  3KB  |  117 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "C:h.kernel"
  5.  
  6. #include "^.151.100.set"
  7.  
  8. void initAdhesive(void); /* defined in Adh.c */
  9.  
  10.  
  11.  
  12.  
  13. void mySetCallProc(void *element, void *private)
  14. {
  15.   printf("%s: %c\n",(char *)private,*((char *)element));
  16. }
  17.  
  18.  
  19.  
  20. int main()
  21. {
  22.   char *a,*b,*c,*d;    /* elements */
  23.   Set  A,B,C,D,X,Y,Z;    /* some sets */
  24.  
  25.   /* intialise Adhesive stuff */
  26.   initAdhesive();
  27.  
  28.   /* We shall create 4 elements a,b,c,d. Each element will
  29.      one character so that we (the humans) can easily see which
  30.      element is which.
  31.      Each of these elements is in a set of 1. Note that the
  32.      element (not the Set) is returned by set_Make();
  33.   */
  34.   a = set_Make(sizeof(char));
  35.   b = set_Make(sizeof(char));
  36.   c = set_Make(sizeof(char));
  37.   d = set_Make(sizeof(char));
  38.   if (!a || !b || !c || !d) {
  39.     printf("out of memory\n");
  40.     exit(EXIT_FAILURE);
  41.   }
  42.  
  43.   /* We shall assign the letters a-d to these elements */
  44.   *a='a';
  45.   *b='b';
  46.   *c='c';
  47.   *d='d';
  48.  
  49.   /* We shall now find out the set each element is in.
  50.      (This will also possibly rearrange internal data to
  51.      optimise performance - path compression, in this case
  52.      though there's nothing to optimise.)
  53.   */
  54.   A = set_Find(a);
  55.   B = set_Find(b);
  56.   C = set_Find(c);
  57.   D = set_Find(d);
  58.  
  59.   /* We shall now output all the elements in set A. */
  60.   set_Call(A,mySetCallProc,"set A");
  61.  
  62.   /* We shall now let X = A union B. This means set A and B
  63.      are no longer accessible. (In fact C=A or C=B but we don't
  64.      really care about this - the header file gives more details
  65.      about this.)
  66.   */
  67.   X = set_Union(A,B);
  68.  
  69.   /* We shall do something similar for Y = C union D. */
  70.   Y = set_Union(C,D);
  71.  
  72.   /* check that a and b are in the same set */
  73.   if (set_Find(a)==set_Find(b)) {
  74.     printf("a and b are in the same set\n");
  75.   } else {
  76.     printf("a and b are not in the same set\n");
  77.   }
  78.  
  79.   /* we shall now join set X and Y to make Z */
  80.   Z = set_Union(X,Y);
  81.  
  82.   /* we shall assign the value associated with set Z to point to a string */
  83.   set_SetValue(Z,"this is a test");
  84.  
  85.   /* We shall check the a and d are in the same set. If we were
  86.      to do this same operation many times we would in fact find that
  87.      the first time is slower than the others. This is because the
  88.      first time round path compression is done whilst searching the
  89.      data structure. If you don't understand this you could look at
  90.      reference 1 (see the set.h file) - or you could ignore this fact.
  91.      In a much larger set this path compression will enable calls
  92.      to set_Find() on the same set (ie Z) to occur much faster.
  93.   */
  94.   if (set_Find(a)==set_Find(d)) {
  95.     printf("a and d are in the same set\n");
  96.   } else {
  97.     printf("a and d are not in the same set\n");
  98.   }
  99.  
  100.  
  101.   /* We now output all elements in the set Z. */
  102.   set_Call(Z,mySetCallProc,"set Z");
  103.  
  104.   /* we shall output the string which Z's value points to */
  105.   printf("%s\n",set_GetValue(Z));
  106.  
  107.  
  108.   /* finally we can dispose of set Z, though this isn't
  109.      strictly important as our program is about to terminate
  110.      anyway
  111.   */
  112.   set_Dispose(Z);
  113.  
  114.  
  115.   return EXIT_SUCCESS;
  116. }
  117.